package com.zte.shopping.action.backend;

import com.zte.shopping.constant.ResponseCodeConstant;
import com.zte.shopping.entity.Product;
import com.zte.shopping.entity.ProductType;
import com.zte.shopping.service.ProductService;
import com.zte.shopping.service.ProductTypeService;
import com.zte.shopping.util.PageSupport;
import com.zte.shopping.util.ResponseResult;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: ssm
 * @description: 后台产品管理界面
 * @author: zuti
 * @create: 2020-06-23 22:09
 **/
@Controller
@RequestMapping("product")
public class BackendProductManagerController {

    private  static  final Logger logger= Logger.getLogger(BackendProductManagerController.class);
    @Autowired
    ProductService productService;

    @Autowired
    ProductTypeService productTypeService;

    @RequestMapping("/toProduct")
    public String  toProductManager( @RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,
                                     Model model){

        logger.debug("enter BackendProductTypeManagerController------> toProductType param: pageIndex"+pageIndex);
        List<Product> productList = productService.productList(pageIndex);

        PageSupport pageSupport = new PageSupport();
        pageSupport.setTotalCount(this.productService.productListCount());
        pageSupport.setCurrentPageNo(pageIndex);

        List<ProductType> typeList = productTypeService.allProductTypeList();
        model.addAttribute("pageSupport", pageSupport);
        model.addAttribute("ProductList", productList);
        model.addAttribute("typeList", typeList);


        return "backend/productManager";
    }

    /**
     * 跳到商品修改页面
     */
    @ResponseBody
    @RequestMapping("/findById")
    public ResponseResult findById(String id)
    {
        ResponseResult result = new ResponseResult();

        // 根据id查询商品信息
        Product product = productService.findById(id);

        result.setMessage("成功");
        result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        result.setReturnObject(product);

        return result;
    }

    /**
     * 商品修改操作
     */
    @RequestMapping("/modifyProduct")
    public ModelAndView modifyProduct(
            @RequestParam("file") CommonsMultipartFile file,
            @RequestParam("modifyId") String productId,
            @RequestParam("modifyName") String name,
            @RequestParam("modifyPrice") String price,
            @RequestParam("modifyTypeId") String typeId,
            HttpSession session,
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") String pageNo)
    {

        ModelAndView modelAndView = new ModelAndView();

        try
        {
            productService.modifyProduct(file, productId, name, price, typeId, session);
        } catch (FileUploadException e)
        {
            modelAndView.addObject("productMsg", e.getMessage());
        }

        modelAndView.setViewName("redirect:/product/toProduct?pageIndex="+pageNo);
        return modelAndView;
    }

    /**
     *删除
     * @param id
     * @param pageNo
     * @return
     */
    @RequestMapping("/removeById")
    public String removeById(
            @RequestParam("id") String id,
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") String pageNo)
    {
        productService.removeById(id);
        return "redirect:/product/toProduct?pageIndex="+pageNo;
    }
    /**
     *	添加商品信息
     * 文件上传操作
     */
    @RequestMapping("/addProduct")
    public ModelAndView addProduct(
            @RequestParam("file") CommonsMultipartFile file,
            @RequestParam(value = "addProductNo",required = false) String productNo,
            @RequestParam(value = "addProductName",required = false) String productName,
            @RequestParam(value = "addProductPrice",required = false) String price,
            @RequestParam(value = "typeId",required = false) String typeId,
            HttpSession session,
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageNo
    )
    {
        ModelAndView modelAndView = new ModelAndView();

        try
        {
            productService.addProduct(file, productNo, productName, price, typeId, session);
        } catch (FileUploadException e)
        {
            modelAndView.addObject("productMsg", e.getMessage());
        }
        modelAndView.setViewName("redirect:/product/toProduct");
        return modelAndView;
    }

}
